package com.liu.web.config;

import com.liu.web.filter.MyFilter;
import com.liu.web.filter.MyServlet;
import com.liu.web.filter.MyServletContextListener;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;

/**
 * Web原生组件注入（Servlet、Filter、listener）
 * 1、使用Servlet API
 * @ServletComponentScan(basePackages = "com.atguigu.admin") :指定原生Servlet组件都放在那里
 * @WebServlet(urlPatterns = "/my")：效果：直接响应，没有经过Spring的拦截器？
 * @WebFilter(urlPatterns={"/css/*","/images/*"})
 * @WebListener
 *
 * spring方式
 */
@Configuration
public class MyRegisterConfig {

    /**
     * servlet注入
     * @return
     */
    @Bean
    public ServletRegistrationBean<MyServlet> myServlet(){
        MyServlet myServlet = new MyServlet();
        return new ServletRegistrationBean(myServlet,"/myServlet");
    }

    /**
     * filter注入
     * @return
     */
    @Bean
    public FilterRegistrationBean myFilter(){
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new MyFilter());
        filterRegistrationBean.setUrlPatterns(Arrays.asList("/*"));
        return filterRegistrationBean;
    }

    /**
     * listener注入
     * @return
     */
    @Bean
    public ServletListenerRegistrationBean myListener(){
        MyServletContextListener myServletContextListener = new MyServletContextListener();
        return new ServletListenerRegistrationBean(myServletContextListener);
    }
}
